<template>
  <div class="search-keyboard" @click.stop>
    <!-- 数字行 (所有模式都显示) -->
    <div class="keyboard-row">
      <button
        class="key-btn"
        v-for="n in 10"
        :key="n % 10"
        @click="onInput((n % 10).toString())"
      >
        {{ n % 10 }}
      </button>
      <button class="key-btn delete-btn" @click="onDelete">退格</button>
    </div>
    <!-- 英文键盘：保留原 QWERTY 排列 -->
    <template v-if="mode === 'english'">
      <div class="keyboard-row">
        <button
          class="key-btn"
          v-for="c in 'QWERTYUIOP'.split('')"
          :key="c"
          @click="onInput(c)"
        >
          {{ c }}
        </button>
        <button class="key-btn clear-btn" @click="onClear">清除</button>
      </div>
      <div class="keyboard-row">
        <button
          class="key-btn"
          v-for="c in 'ASDFGHJKL、'.split('')"
          :key="c"
          @click="onInput(c)"
        >
          {{ c }}
        </button>
        <button class="key-btn switch-btn" @click="onSwitch">切换</button>
      </div>
      <div class="keyboard-row">
        <button
          class="key-btn"
          v-for="c in 'ZXCVBNM,。;'.split('')"
          :key="c"
          @click="onInput(c)"
        >
          {{ c }}
        </button>
        <button class="key-btn space-btn" @click="onInput(' ')">空格</button>
      </div>
    </template>
    <!-- 中文拼音键盘：改成按 A-Z 顺序（大写） -->
    <template v-else-if="mode === 'chinese'">
      <div class="keyboard-row" v-for="(rowChars, rIdx) in alphaRows" :key="rIdx">
        <button
          class="key-btn"
          v-for="c in rowChars.split('')"
          :key="c"
          @click="onInput(c)"
        >
          {{ c }}
        </button>
        <button
          v-if="rIdx === alphaRows.length - 1"
          class="key-btn space-btn"
          @click="onInput(' ')"
        >空格</button>
        <button
          v-if="rIdx === 0"
          class="key-btn clear-btn"
          @click="onClear"
        >清除</button>
        <button
          v-if="rIdx === 1"
          class="key-btn switch-btn"
          @click="onSwitch"
        >切换</button>
      </div>
    </template>
  </div>
</template>

<script lang="ts" setup>
import { computed, defineProps, defineEmits } from 'vue';
// 只保留 english 与 chinese 两种模式
const props = defineProps<{ mode: 'english' | 'chinese' }>();
const emit = defineEmits(['input', 'delete', 'clear', 'switch']);

// 中文拼音模式下按 A-Z 顺序分三行展示
const alphaRows = ['ABCDEFGHIJ', 'KLMNOPQRS、', 'TUVWXYZ，。；'];

const modeDisplayName = computed(() => {
  switch (props.mode) {
    case 'english':
      return '英文输入';
    case 'chinese':
      return '中文拼音';
    default:
      return '未知模式';
  }
});

function onInput(char: string) {
  emit('input', char);
}
function onDelete() {
  emit('delete');
}
function onClear() {
  emit('clear');
}
function onSwitch() {
  // 外部只需切换 english <-> chinese
  emit('switch');
}
</script>

<style scoped>
.search-keyboard {
  /* 复用 prescription.vue 的样式 */
  position: absolute;
  top: -220px;
  left: 200px;
  z-index: 9999;
  background: #f8f9fa;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  width: 600px;
  max-width: 90vw;
}
.keyboard-mode-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: white;
  border-radius: 6px;
  border: 1px solid #e4e7ed;
}
.mode-text {
  font-size: 14px;
  color: #666;
  margin-right: 8px;
}
.mode-badge {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  color: white;
}
.mode-english {
  background: #409eff;
}
.mode-chinese {
  background: #67c23a;
}
/* symbols 模式已删除 */
.keyboard-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
  justify-content: center;
}
.key-btn {
  min-width: 45px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  background: #fff;
  color: #333;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 8px;
}
.key-btn:hover {
  background: #e9ecef;
  border-color: #409eff;
}
.key-btn:active {
  background: #dee2e6;
  transform: scale(0.95);
}
.key-btn.delete-btn,
.key-btn.clear-btn,
.key-btn.switch-btn,
.key-btn.space-btn {
  min-width: 60px;
  color: #000;
}
</style>
